package com.yanqu.road.dao.impl.activity.stronghold;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.stronghold.CrossStrongholdEliminateDamage;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossStrongholdEliminateDamageDaoImpl extends TempDataObjectDao<CrossStrongholdEliminateDamage> {
    public static String getCrossStrongholdEliminateDamageSql = "select * from t_c_activity_stronghold_eliminate_damage where activityId=?";
    public Map<String, Map<Long, Map<String, CrossStrongholdEliminateDamage>>> getCrossStrongholdEliminateDamage(int activityId,int round) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, Map<Long, Map<String, CrossStrongholdEliminateDamage>>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossStrongholdEliminateDamageSql);
            int i = 1;
            call.setInt(i++,activityId);
            call.setInt(i++,round);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossStrongholdEliminateDamage damageRecord = parse(rs);
                long serverId = damageRecord.getServerId();
                String unionUid = damageRecord.getUnionUid();
                long userId = damageRecord.getUserId();

                Map<Long,  Map<String, CrossStrongholdEliminateDamage>> attackUserMap = dataMap.get(unionUid);
                if (attackUserMap == null) {
                    attackUserMap = new ConcurrentHashMap<>(16);
                    dataMap.put(unionUid, attackUserMap);
                }
                Map<String, CrossStrongholdEliminateDamage> enemyServerMap = attackUserMap.get(damageRecord.getUserId());
                if (enemyServerMap == null) {
                    enemyServerMap = new ConcurrentHashMap<>();
                    attackUserMap.put(damageRecord.getUserId(), enemyServerMap);
                }
                enemyServerMap.put(damageRecord.getEnemyUnionUid(), damageRecord);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public List<CrossStrongholdEliminateDamage> getEliminateDamageRecordList(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<CrossStrongholdEliminateDamage> dataList = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossStrongholdEliminateDamageSql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossStrongholdEliminateDamage damageRecord = parse(rs);
                dataList.add(damageRecord);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public static String updateCrossStrongholdEliminateDamageSql = "update t_c_activity_stronghold_eliminate_damage set unionUid=?,userBaseInfo=?,damage=? where activityId=? and round=? and serverId=? and userId=? and enemyServerId=? and enemyUnionUid=?";
    @Override
    protected boolean updateData(CrossStrongholdEliminateDamage crossStrongholdEliminateDamage) {
        crossStrongholdEliminateDamage.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, crossStrongholdEliminateDamage.getUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossStrongholdEliminateDamage.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, crossStrongholdEliminateDamage.getDamage().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, crossStrongholdEliminateDamage.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossStrongholdEliminateDamage.getRound()));
        para.put(i++, new DbParameter(Types.BIGINT, crossStrongholdEliminateDamage.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossStrongholdEliminateDamage.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossStrongholdEliminateDamage.getEnemyServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossStrongholdEliminateDamage.getEnemyUnionUid()));
        if (getDb().executeNoneQuery(updateCrossStrongholdEliminateDamageSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossStrongholdEliminateDamageSql = "insert into t_c_activity_stronghold_eliminate_damage(activityId,round,serverId,unionUid,userId,userBaseInfo,enemyServerId,enemyUnionUid,damage) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossStrongholdEliminateDamage crossStrongholdEliminateDamage) {
        crossStrongholdEliminateDamage.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossStrongholdEliminateDamage.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossStrongholdEliminateDamage.getRound()));
        para.put(i++, new DbParameter(Types.BIGINT, crossStrongholdEliminateDamage.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossStrongholdEliminateDamage.getUnionUid()));
        para.put(i++, new DbParameter(Types.BIGINT, crossStrongholdEliminateDamage.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossStrongholdEliminateDamage.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, crossStrongholdEliminateDamage.getEnemyServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossStrongholdEliminateDamage.getEnemyUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossStrongholdEliminateDamage.getDamage().toString()));
        if (getDb().executeNoneQuery(insertCrossStrongholdEliminateDamageSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossStrongholdEliminateDamage parseData(ResultSet rs) throws SQLException {CrossStrongholdEliminateDamage crossStrongholdEliminateDamage = new CrossStrongholdEliminateDamage();
        crossStrongholdEliminateDamage.setActivityId(rs.getInt("activityId"));
        crossStrongholdEliminateDamage.setRound(rs.getInt("round"));
        crossStrongholdEliminateDamage.setServerId(rs.getLong("serverId"));
        crossStrongholdEliminateDamage.setUnionUid(rs.getString("unionUid"));
        crossStrongholdEliminateDamage.setUserId(rs.getLong("userId"));
        crossStrongholdEliminateDamage.setUserBaseInfo(JSON.parseObject(rs.getString("userBaseInfo"), UserBaseInfo.class));
        crossStrongholdEliminateDamage.setEnemyServerId(rs.getLong("enemyServerId"));
        crossStrongholdEliminateDamage.setEnemyUnionUid(rs.getString("enemyUnionUid"));
        crossStrongholdEliminateDamage.setDamage(new BigInteger(rs.getString("damage")));
        return crossStrongholdEliminateDamage;
    }

}
